package cz.data.common.support.db.core;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Data
public class DbTable {

    private String dbName;
    private String schemaName;

    @NotBlank(message = "表名不能为空")
    private String tableName;
    @NotBlank(message = "表注释不能为空")
    private String tableComment;

    /**
     * 表类型 表/视图
     */
    private String tableType = Type.Table.value;

    /**
     * 表列
     */
    @Valid
    private List<DbColumn> columns;

    /**
     * 表配置
     */
    @JsonIgnore
    private String settings;

    public static DbTable from(String tableName, String tableComment) {
        DbTable table = new DbTable();
        table.setTableName(tableName);
        table.setTableComment(tableComment);
        return table;
    }

    @Getter
    @AllArgsConstructor
    public enum Type {
        Table("1"),
        View("2")
        ;

        private final String value;

        public boolean equalsValue(String value) {
            return this.value.equals(value);
        }
    }
}
